package com.info.dbase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

/**
 * @author HAHA
 * 
 */
public class ExecuteSql {
	private static Logger logger = Logger.getLogger(ExecuteSql.class);

	/**
	 * 查询结果集
	 * @param sql
	 * @param conn
	 * @return
	 * @throws SQLException
	 * @throws Exception
	 */
	public ResultSet getRs(String sql, Connection conn,Statement stmt) throws SQLException,
			Exception {
		//Statement stmt = null;
		//ResultSet rs = null;
		try {
			if(stmt==null){
			stmt = conn.createStatement();}
			return stmt.executeQuery(sql);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			throw e;
		} catch (Exception e1) {
			System.out.println(e1.getMessage());
			throw e1;
		}
		
	}

	/**
	 * 执行sql
	 * @param sql
	 * @param conn
	 * @throws SQLException
	 * @throws Exception
	 */
	public void executeSql(String sql, Connection conn) throws SQLException,
			Exception {
		Statement stmt = null;
		try {
			stmt = conn.createStatement(1004, 1007);
			stmt.executeUpdate(sql);
			stmt.close();
		} catch (SQLException e) {
			throw e;
		} catch (Exception e1) {
			throw e1;
		}
	}

	/**
	 * 执行sql集
	 * @param sql
	 * @param conn
	 * @throws SQLException
	 * @throws Exception
	 */
	public void executeSqlBat(String[] sql, Connection conn)
			throws SQLException, Exception {
		if (sql != null || sql.length > 0) {
			Statement stmt = null;
				conn.setAutoCommit(false);
				stmt = conn.createStatement(1004, 1007);
				for (int i = 0; i < sql.length; i++) {
					if (sql[i] != null || !sql[i].equals("")) {
						stmt.executeUpdate(sql[i]);
					}
				}
				conn.commit();
				stmt.close();
			
		}
	}
}
